package com.jk.controller;

import com.jk.dto.TbAdmin;
import com.jk.pojo.PagedResponse;
import com.jk.pojo.ResponseResult;

import com.jk.service.TbAdminServiceFegin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author yzf
 * @version 1.0
 * @description: TODO
 * @date 2024/12/9 20:18
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping(value = "tbadmin")
public class TbAdminController {
    @Autowired
    private TbAdminServiceFegin tbAdminServiceFegin;

    @ApiOperation(value = "查询",httpMethod = "POST",notes = "")
    @PostMapping(value = "findTbAdminList")
    public ResponseResult<PagedResponse<TbAdmin>> findTbAdminList(@RequestBody TbAdmin tbAdmin, @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult(200,"查询成功",tbAdminServiceFegin.findTbAdminList(tbAdmin,pageNum,pageSize));
    }

    @ApiOperation(value = "删除",httpMethod = "DELETE",notes = "删除")
    @DeleteMapping("deleteTbAdminById")
    public ResponseResult deleteTbAdminById(@RequestParam Integer id) {

        try {
            tbAdminServiceFegin.deleteTbAdminById(id);
            return new ResponseResult<>(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(500,"删除失败");

        }
    }


    @ApiOperation(value = "新增",httpMethod = "POST",notes = "新增")
    @RequestMapping("addTbAdmin")
    public ResponseResult addTbAdmin(@RequestBody TbAdmin tbAdmin) {
        try {
            //密码加密
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(tbAdmin.getPassword());
            tbAdmin.setPassword(encode);
            tbAdmin.setCreateTime(new Date());

            tbAdminServiceFegin.addTbAdmin(tbAdmin);
            return new ResponseResult<>(200,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(500,"新增失败");
        }
    }

    @ApiOperation(value = "回显",httpMethod = "GET",notes = "回显")
    @RequestMapping("getTbAdminById")
    public ResponseResult<TbAdmin> getTbAdminById(@RequestParam Integer id) {
        return new ResponseResult<>(200,"回显成功", tbAdminServiceFegin.getTbAdminById(id));
    }

    /**
     * @description: 修改
     * @param: [sysUser]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author yzf
     * @date: 2024/11/5 20:22
     */

    @ApiOperation(value = "修改",httpMethod = "PUT",notes = "修改")
    @RequestMapping("upTbAdmin")
    public ResponseResult upTbAdmin(@RequestBody TbAdmin tbAdmin) {
//        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        tbAdmin.setModifyUser(loginUser.getUser().getUserName());
        tbAdmin.setModifyTime(new Date());
        try {
            tbAdminServiceFegin.upTbAdmin(tbAdmin);
            return new ResponseResult<>(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(500,"修改失败");

        }
    }

    @ApiOperation(value = "上下架",httpMethod = "POST",notes = "修改")
    @PostMapping("upstatus")
    public ResponseResult upstatus(@RequestBody TbAdmin tbAdmin) {
        try {
            tbAdminServiceFegin.upstatus(tbAdmin);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    @ApiOperation(value = "上下架",httpMethod = "POST",notes = "修改")
    @PostMapping("updatestatus")
    public ResponseResult updatestatus(@RequestBody TbAdmin tbAdmin) {
        try {
            tbAdminServiceFegin.updatestatus(tbAdmin);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }

}

